#include "StdAfx.h"
#include "SimpleObj.h"


u8 CSimpleObj::m_texture[8*8] = {
	0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8,
	0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9,
	0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8,
	0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9,
	0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8,
	0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9,
	0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8,
	0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9, 0xf8, 0xf9
};
			
u16 CSimpleObj::m_pal[8] = 
	{0xfC00, 0xffff,0x0000, 0xffff,0x0000, 0xffff,0x0000, 0xffff};

void CSimpleObj::renderCube()
{
    G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
                   GX_POLYGONMODE_MODULATE, // modulation mode
                   GX_CULL_BACK,	//cull none
                   0,              // polygon ID(0 - 63)
                   31,             // alpha(0 - 31)
                   0               // OR of GXPolygonAttrMisc's value
        );


    G3_Begin(GX_BEGIN_QUADS);

	//front face
	G3_TexCoord(0,0);
	G3_Normal(0, 0, FX16_ONE-1);
	G3_Vtx(-FX16_HALF,  FX16_HALF,  FX16_HALF);

	G3_TexCoord(0,FX32_ONE);
	G3_Vtx(-FX16_HALF, -FX16_HALF,  FX16_HALF);

	G3_TexCoord(FX32_ONE,FX32_ONE);
	G3_Vtx( FX16_HALF, -FX16_HALF,  FX16_HALF);

	G3_TexCoord(FX32_ONE,0);
	G3_Vtx( FX16_HALF,  FX16_HALF,  FX16_HALF);

	//back face
	G3_TexCoord(0,0);
	G3_Normal(0, 0, -FX16_ONE+1);
	G3_Vtx( FX16_HALF,  FX16_HALF, -FX16_HALF);

	G3_TexCoord(0,FX32_ONE);
	G3_Vtx( FX16_HALF, -FX16_HALF, -FX16_HALF);

	G3_TexCoord(FX32_ONE,FX32_ONE);
	G3_Vtx(-FX16_HALF, -FX16_HALF, -FX16_HALF);

	G3_TexCoord(FX32_ONE,0);
	G3_Vtx(-FX16_HALF,  FX16_HALF, -FX16_HALF);

	//right face
	G3_TexCoord(0,0);
	G3_Normal(FX16_ONE-1, 0, 0);
	G3_Vtx( FX16_HALF,  FX16_HALF,  FX16_HALF);

	G3_TexCoord(0,FX32_ONE);
	G3_Vtx( FX16_HALF, -FX16_HALF,  FX16_HALF);

	G3_TexCoord(FX32_ONE,FX32_ONE);
	G3_Vtx( FX16_HALF, -FX16_HALF, -FX16_HALF);

	G3_TexCoord(FX32_ONE,0);
	G3_Vtx( FX16_HALF,  FX16_HALF, -FX16_HALF);

	//left face
	G3_TexCoord(0,0);
	G3_Normal(-FX16_ONE+1, 0, 0);
	G3_Vtx(-FX16_HALF,  FX16_HALF, -FX16_HALF);
	
	G3_TexCoord(0,FX32_ONE);
	G3_Vtx(-FX16_HALF, -FX16_HALF, -FX16_HALF);
	
	G3_TexCoord(FX32_ONE,FX32_ONE);
	G3_Vtx(-FX16_HALF, -FX16_HALF,  FX16_HALF);
	
	G3_TexCoord(FX32_ONE,0);
	G3_Vtx(-FX16_HALF,  FX16_HALF,  FX16_HALF);

	//top face
	G3_TexCoord(0,0);
	G3_Normal(0, FX16_ONE-1, 0);
	G3_Vtx(-FX16_HALF,  FX16_HALF, -FX16_HALF);
	
	G3_TexCoord(0,FX32_ONE);
	G3_Vtx(-FX16_HALF,  FX16_HALF,  FX16_HALF);
	
	G3_TexCoord(FX32_ONE,FX32_ONE);
	G3_Vtx( FX16_HALF,  FX16_HALF,  FX16_HALF);
	
	G3_TexCoord(FX32_ONE,0);
	G3_Vtx( FX16_HALF,  FX16_HALF, -FX16_HALF);
	
	//bottom face
	G3_TexCoord(0,0);
	G3_Normal(0, -FX16_ONE+1, 0);
	G3_Vtx(-FX16_HALF, -FX16_HALF,  FX16_HALF);
	
	G3_TexCoord(0,FX32_ONE);
	G3_Vtx(-FX16_HALF, -FX16_HALF, -FX16_HALF);
	
	G3_TexCoord(FX32_ONE,FX32_ONE);
	G3_Vtx( FX16_HALF, -FX16_HALF, -FX16_HALF);
	
	G3_TexCoord(FX32_ONE,0);
	G3_Vtx( FX16_HALF, -FX16_HALF,  FX16_HALF);
	
    G3_End();
}

void CSimpleObj::simpleLight()
{
//	G3_LightVector(GX_LIGHTID_0, -FX16_SQRT1_3, FX16_SQRT1_3, FX16_SQRT1_3);
	G3_LightVector(GX_LIGHTID_0, 0, 0, -FX16_ONE + 1);
	G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
}

void CSimpleObj::simpleMaterial()
{
	G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // diffuse
	                    GX_RGB(8, 8, 8),     // ambient
	                    TRUE   // use diffuse as vtx color if TRUE
	);
	
	G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
	                    GX_RGB(0, 0, 0),        // emission
	                    FALSE  // use shininess table if TRUE
	);
}

void CSimpleObj::simpleCamera()
{
	VecFx32 Eye = { 0, 0, FX32_ONE };   // Eye position
	VecFx32 at = { 0, 0, 0 };  // Viewpoint
	VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
	
	G3_LookAt(&Eye, &vUp, &at, NULL);	
}

void CSimpleObj::textureScale(int s, int t)
{
    G3_MtxMode(GX_MTXMODE_TEXTURE);
    G3_Identity();
    G3_Scale(s * FX32_ONE, t * FX32_ONE, 1);
    // Use a scaled matrix for the texture matrix
    G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
}

u32 addr = 0x0000;
void CSimpleObj::buildCheckerTex()
{
	GX_BeginLoadTex();
	GX_LoadTex(m_texture, addr, 64);
	GX_EndLoadTex();
	
	GX_BeginLoadTexPltt();
	GX_LoadTexPltt(m_pal, 0x0000 ,16);
	GX_EndLoadTexPltt();  
}

void CSimpleObj::useCheckerTex()
{
        G3_TexImageParam(GX_TEXFMT_ALPHA,       // use alpha texture
                     GX_TEXGEN_TEXCOORD,    // use texcoord
                     GX_TEXSIZE_S8,        // 16 pixels
                     GX_TEXSIZE_T8,        // 16 pixels
                     GX_TEXREPEAT_ST,     // no repeat
                     GX_TEXFLIP_NONE,       // no flip
                     GX_TEXPLTTCOLOR0_USE,  // use color 0 of the palette
                     addr     // the offset of the texture image
        );

    G3_TexPlttBase(0x0000,  // the offset of the texture palette
                   GX_TEXFMT_ALPHA // alpha texture
        );
}


